package com.centling.smartSealForPhone.activity

import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import android.text.TextUtils
import android.view.View
import com.centling.smartSealForPhone.R
import com.centling.smartSealForPhone.bean.AuditorReviewDetailBean
import com.centling.smartSealForPhone.constants.SPConstants
import com.centling.smartSealForPhone.constants.UrlConstants
import com.centling.smartSealForPhone.utils.ImageUtil
import com.centling.smartSealForPhone.utils.OkHttpUtil
import com.centling.smartSealForPhone.utils.SPUtil
import com.centling.smartSealForPhone.utils.ShowToast
import com.example.localalbum.common.LocalImageHelper
import com.example.localalbum.ui.PagerView
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_auditor_seal_detail.*
import java.io.Serializable
import java.util.*

/**
 * Created by Jessie on 2016/8/16.
 */
class AuditorSealDetailActivity : TitleBarActivity() {

    private var auditorReviewDetailBean: AuditorReviewDetailBean? = null
    private var files = ArrayList<String>()
    private var failFiles = ArrayList<String>()
    private var statusInfo = ""
    private var dataStatus = ""
    private var reviewResultType = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_auditor_seal_detail)
        setTitleBarText("用印详情")

        initListener()
        tv_auditor_detail_cancel.setOnClickListener { finish() }
    }

    private fun sendRequest() {
//        val apply_type: String = intent.getStringExtra("TYPE_APPLY")
        val id: String = intent.getStringExtra("sealApplyId")
        getEnqueue(UrlConstants.GET_CHECK_DETAIL + id, object : OkHttpUtil.NetCallBack {
            override fun onSucceed(json: String?) {
                auditorReviewDetailBean = Gson().fromJson(json, AuditorReviewDetailBean::class.java)

                when {
                    auditorReviewDetailBean?.isButtonShow.equals("0") -> {
                        //审批
                        tv_auditor_first_instance.visibility = View.VISIBLE
                        tv_auditor_review.visibility = View.GONE
                    }
                    auditorReviewDetailBean?.isButtonShow.equals("1") -> {
                        //复审
                        tv_auditor_first_instance.visibility = View.GONE
                        tv_auditor_review.visibility = View.VISIBLE
                    }
                    else -> {
                        //审核完成
                        tv_auditor_first_instance.visibility = View.GONE
                        tv_auditor_review.visibility = View.GONE
                    }
                }

                if (!TextUtils.isEmpty(auditorReviewDetailBean!!.curTaster)
                        && !TextUtils.equals(SPUtil.getString(SPConstants.USER_ID), auditorReviewDetailBean!!.curTaster)) {
                    tv_auditor_first_instance.visibility = View.GONE
                    tv_auditor_review.visibility = View.GONE
                }

                when (Integer.parseInt(auditorReviewDetailBean?.status)) {
                    0 -> {
                        statusInfo = "审核中"
                    }
                    1 -> {
                        statusInfo = "使用中"
                    }
                    2 -> {
                        statusInfo = "审核失败"
                    }
                    3 -> {
                        statusInfo = "未审核过期"
                    }
                    4 -> {
                        statusInfo = "审核成功后过期"
                    }
                    5 -> {
                        statusInfo = "用章完成"
                    }
                    6 -> {
                        statusInfo = "复审成功"
                    }
                    7 -> {
                        statusInfo = "复审失败"
                    }
                    8 -> {
                        statusInfo = "补拍完成驳回过期"
                    }
                    9 -> {
                        statusInfo = "驳回"
                    }
                    10 -> {
                        statusInfo = "驳回过期"
                    }

                }
                when (Integer.parseInt(auditorReviewDetailBean?.isValid)) {
                    0 -> {
                        dataStatus = "正常"
                    }
                    1 -> {
                        dataStatus = "换章失效"
                    }
                    2 -> {
                        dataStatus = "解除注册失效"
                    }
                }
                if (auditorReviewDetailBean?.reviewResultType.equals("ILLEGAL")) {
                    reviewResultType = "非法"
                } else {
                    reviewResultType = "补拍"
                }
                tv_auditor_seal_detail_name.text = auditorReviewDetailBean?.applicantUserName
                ImageUtil.loadAvatarImage(auditorReviewDetailBean?.applicantUserAvatar, riv_auditor_seal_detail_avatar, R.drawable.iv_avatar_default)
                riv_auditor_seal_detail_avatar.setOnClickListener {
                    val pictures: MutableList<LocalImageHelper.LocalFile> = ArrayList()
                    val file = LocalImageHelper.LocalFile()
                    file.originalUri = auditorReviewDetailBean?.applicantUserAvatar
                    file.thumbnailUri = auditorReviewDetailBean?.applicantUserAvatar
                    pictures.add(file)
                    val intent = Intent(mContext, PagerView::class.java)
                    intent.putExtra("FILE_LIST", pictures as Serializable)
                    intent.putExtra("INDEX", 0)
                    mContext.startActivity(intent)
                }
                tv_auditor_seal_detail_file_count.text = auditorReviewDetailBean?.docPicsCount.toString()

                tv_auditor_seal_detail_approval_history.setOnClickListener {
                    if (auditorReviewDetailBean != null
                            && auditorReviewDetailBean!!.sealApplyAuditRecordList != null
                            && !auditorReviewDetailBean!!.sealApplyAuditRecordList.isEmpty()) {
                        startActivity(Intent(mContext, HistoryApprovalRecordsActivity::class.java).putParcelableArrayListExtra("history_stamp_records",
                                auditorReviewDetailBean!!.sealApplyAuditRecordList as ArrayList<out Parcelable>))
                    } else {
                        ShowToast.shortTime("没有审阅记录")
                    }
                }

                tv_auditor_seal_detail_apply_time.text = auditorReviewDetailBean?.applyDate
                tv_auditor_seal_detail_residue_times.text = auditorReviewDetailBean?.remainTimes.toString()
                tv_auditor_seal_detail_apply_times.text = auditorReviewDetailBean?.applyTimes.toString()
                tv_auditor_seal_start_time.text = auditorReviewDetailBean?.applyStartTimeReal
                tv_auditor_seal_end_time.text = auditorReviewDetailBean?.applyEndTimeReal
                files.clear()
                isv_auditor_detail_picture.files.clear()
                for (file: String in auditorReviewDetailBean?.docPicList!!) {
                    files.add(file)
                }
                isv_auditor_detail_picture.addFiles(files)
                tv_auditor_seal_detail_status.text = statusInfo
                tv_auditor_seal_detail_data_status.text = dataStatus
                tv_auditor_seal_detail_submit.text = auditorReviewDetailBean?.applyOperator
                if (auditorReviewDetailBean?.applyType.equals("COUNT_LIMIT")) {
                    lv_count_use_time.visibility = View.VISIBLE
                    tv_auditor_seal_detail_seal_type.text = "限次"

                } else {
                    lv_count_use_time.visibility = View.GONE
                    tv_auditor_seal_detail_seal_type.text = "限时"
                }
                if(auditorReviewDetailBean?.embedEquipOutFlag.equals("1")){
                    tv_auditor_seal_detail_seal_need_take.text = "是"
                }else{
                    tv_auditor_seal_detail_seal_need_take.text = "否"
                }
                tv_auditor_seal_detail_seal_name.text = auditorReviewDetailBean?.sealName
                tv_auditor_seal_detail_org.text = auditorReviewDetailBean?.sealOrg
                tv_auditor_seal_detail_seal_reason.text = auditorReviewDetailBean?.applyReasonType
                tv_auditor_seal_detail_remark.text = auditorReviewDetailBean?.applyReason
                tv_auditor_seal_start_time.text = auditorReviewDetailBean?.applyStartTimeReal
                tv_auditor_seal_end_time.text = auditorReviewDetailBean?.applyEndTimeReal
                //审批
                tv_auditor_seal_detail_first_instance_current.text = auditorReviewDetailBean?.curTasterName ?: ""
                tv_auditor_seal_detail_first_instance.text = auditorReviewDetailBean?.authOperator
                tv_auditor_seal_detail_first_instance_time.text = auditorReviewDetailBean?.authTimeReal
                tv_auditor_seal_detail_first_lose_reason.text = auditorReviewDetailBean?.authFailReason
                //复审
                tv_auditor_seal_detail_review_operator.text = auditorReviewDetailBean?.reviewOperator
                tv_auditor_seal_detail_review_time.text = auditorReviewDetailBean?.reviewcreatetimeReal
                tv_auditor_seal_detail_review_lose_type.text = reviewResultType
                tv_auditor_seal_detail_reshoot_count.text = auditorReviewDetailBean?.picCount.toString()
                failFiles.clear()
                sv_auditor_seal_detail_review_lose_pic.files.clear()
                for (file: String in auditorReviewDetailBean?.reviewFailPicsPath!!) {
                    failFiles.add(file)
                }
                sv_auditor_seal_detail_review_lose_pic.addFiles(failFiles)
                tv_auditor_seal_detail_review_lose_reason.text = auditorReviewDetailBean?.reviewFailReason
                //限时限次
                if (auditorReviewDetailBean?.applyType.equals("COUNT_LIMIT")) {
                    ll_limit_time.visibility = View.GONE
                    ll_limit_times.visibility = View.VISIBLE
                } else {
                    ll_limit_time.visibility = View.VISIBLE
                    ll_limit_times.visibility = View.GONE
                }
                //（申请状态，授权状态：0:审核中， 1：使用中，2：审核失败，  3:未审核过期， 4：审核成功后过期， 5 用章完成， 6 复审成功， 7 复审失败， 8补拍完成 ，9:驳回 ， 10:驳回过期）
                if (auditorReviewDetailBean?.status.equals("0")) {//待审批
                    ll_auditor_first_instance.visibility = View.GONE
                    ll_auditor_seal_detail_first_lose.visibility = View.GONE
                    rl_auditor_seal_record.visibility = View.GONE
                    ll_auditor_review.visibility = View.GONE
                } else if (auditorReviewDetailBean?.status.equals("1")) {//审批成功
                    ll_auditor_first_instance.visibility = View.VISIBLE
                    ll_auditor_seal_detail_first_lose.visibility = View.GONE
                    rl_auditor_seal_record.visibility = View.GONE
                    ll_auditor_review.visibility = View.GONE
                } else if (auditorReviewDetailBean?.status.equals("2") || auditorReviewDetailBean?.status.equals("9") || auditorReviewDetailBean?.status.equals("10")) {
                    ll_auditor_first_instance.visibility = View.VISIBLE
                    ll_auditor_seal_detail_first_lose.visibility = View.VISIBLE
                    rl_auditor_seal_record.visibility = View.GONE
                    ll_auditor_review.visibility = View.GONE
                } else if (auditorReviewDetailBean?.status.equals("3")) {
                    ll_auditor_first_instance.visibility = View.GONE
                    ll_auditor_seal_detail_first_lose.visibility = View.GONE
                    rl_auditor_seal_record.visibility = View.GONE
                    ll_auditor_review.visibility = View.GONE
                } else if (auditorReviewDetailBean?.status.equals("4")) {
                    ll_auditor_first_instance.visibility = View.VISIBLE
                    ll_auditor_seal_detail_first_lose.visibility = View.GONE
                    rl_auditor_seal_record.visibility = View.GONE
                    ll_auditor_review.visibility = View.GONE
                } else if (auditorReviewDetailBean?.status.equals("5")) {
                    ll_auditor_first_instance.visibility = View.VISIBLE
                    ll_auditor_seal_detail_first_lose.visibility = View.GONE
                    if (auditorReviewDetailBean?.applyType.equals("COUNT_LIMIT")) {
                        rl_auditor_seal_record.visibility = View.VISIBLE
                    } else {
                        rl_auditor_seal_record.visibility = View.GONE
                    }
                    ll_auditor_review.visibility = View.GONE
                } else if (auditorReviewDetailBean?.status.equals("6")) {
                    ll_auditor_first_instance.visibility = View.VISIBLE
                    ll_auditor_seal_detail_first_lose.visibility = View.GONE
                    if (auditorReviewDetailBean?.applyType.equals("COUNT_LIMIT")) {
                        rl_auditor_seal_record.visibility = View.VISIBLE
                    } else {
                        rl_auditor_seal_record.visibility = View.GONE
                    }
                    ll_auditor_review_success.visibility = View.VISIBLE
                    ll_auditor_review_rePic.visibility = View.GONE

                } else if (auditorReviewDetailBean?.status.equals("7")) {
                    ll_auditor_first_instance.visibility = View.VISIBLE
                    ll_auditor_seal_detail_first_lose.visibility = View.GONE
                    if (auditorReviewDetailBean?.applyType.equals("COUNT_LIMIT")) {
                        rl_auditor_seal_record.visibility = View.VISIBLE
                    } else {
                        rl_auditor_seal_record.visibility = View.GONE
                    }
                    ll_auditor_review_success.visibility = View.VISIBLE
                    if (auditorReviewDetailBean?.reviewResultType.equals("ILLEGAL")) {
                        ll_auditor_review_rePic.visibility = View.VISIBLE
                        tv_auditor_review_lose_pic.visibility = View.GONE
                        ll_auditor_review_lose_pic.visibility=View.GONE
                        sv_auditor_seal_detail_review_lose_pic.visibility = View.GONE
                        ll_auditor_review_repic_count.visibility = View.GONE
                    } else {
                        ll_auditor_review_rePic.visibility = View.VISIBLE
                    }

                } else if (auditorReviewDetailBean?.status.equals("8")) {
                    ll_auditor_first_instance.visibility = View.VISIBLE
                    ll_auditor_seal_detail_first_lose.visibility = View.GONE
                    if (auditorReviewDetailBean?.applyType.equals("COUNT_LIMIT")) {
                        rl_auditor_seal_record.visibility = View.VISIBLE
                    } else {
                        rl_auditor_seal_record.visibility = View.GONE
                    }
                    ll_auditor_review_success.visibility = View.VISIBLE
                    ll_auditor_review_rePic.visibility = View.VISIBLE
                } else if (auditorReviewDetailBean?.status.equals("9") || auditorReviewDetailBean?.status.equals("10")) {
                    ll_auditor_first_instance.visibility = View.VISIBLE
                    ll_auditor_seal_detail_first_lose.visibility = View.GONE
                    if (auditorReviewDetailBean?.applyType.equals("COUNT_LIMIT")) {
                        rl_auditor_seal_record.visibility = View.VISIBLE
                    } else {
                        rl_auditor_seal_record.visibility = View.GONE
                    }
                    ll_auditor_review_success.visibility = View.VISIBLE
                    ll_auditor_review_rePic.visibility = View.VISIBLE
                    tv_auditor_review_lose_pic.visibility = View.GONE
                    sv_auditor_seal_detail_review_lose_pic.visibility = View.GONE
                }
            }

            override fun onFailed(reason: String?) {
                ShowToast.shortTime(reason)
            }
        })

    }

    private fun initListener() {
        tv_auditor_first_instance?.setOnClickListener {
            startActivity(Intent(mContext, AuditorFirstInstanceActivity::class.java).putExtra("sealApplyId", intent.getStringExtra("sealApplyId")))
        }
        tv_auditor_review?.setOnClickListener {
            startActivity(Intent(mContext, AuditorReviewActivity::class.java).putExtra("isButton", true).putExtra("sealApplyId", intent.getStringExtra("sealApplyId")))
        }
        rl_auditor_seal_record.setOnClickListener {
            //盖章记录
            startActivity(Intent(mContext, AuditorReviewActivity::class.java).putExtra("isButton", false).putExtra("sealApplyId", intent.getStringExtra("sealApplyId")))
        }
    }

    override fun onResume() {
        super.onResume()
        sendRequest()
    }
}
